﻿Imports Dainfo.ControleEstoque.Dados
Imports Dainfo.ControleEstoque.Model
Imports System.Globalization

Partial Public Class ConsultarVendas
    Inherits System.Web.UI.Page

    Private _key As String = "Itens"


    Dim dadosVenda As New DadosVenda()
    Function Total(ByVal a As Integer) As Double
        Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("pt-BR")
        Dim precoTotal As Double = 0
        For Each it As ItensVenda In dadosVenda.GetAll(a).Itens
            precoTotal = precoTotal + (it.Produto.ValorVenda * it.Quantidade)
        Next
        Return precoTotal
    End Function

    Public Property Vendas() As List(Of Model.Venda)
        Get
            If ViewState(_key) Is Nothing Then
                ViewState(_key) = New List(Of Model.Venda)
            End If

            Return ViewState(_key)
        End Get
        Set(ByVal value As List(Of Model.Venda))
            ViewState(_key) = value
        End Set
    End Property



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CarregaGrid()
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvVendas.SelectedIndexChanged
        gvItens.DataSource = dadosVenda.GetAll(gvVendas.SelectedIndex).Itens
        gvItens.DataBind()
        CarregaGrid()
    End Sub



    Sub CarregaGrid()
        gvVendas.DataSource = dadosVenda.GetAll()
        gvVendas.DataBind()
    End Sub

    Protected Sub gvItens_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvItens.SelectedIndexChanged

    End Sub

    Protected Sub gvItens_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvItens.RowDataBound
        If e.Row.RowType <> DataControlRowType.DataRow Then
            Return
        End If
        Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("pt-BR")

        Dim iten As ItensVenda = CType(e.Row.DataItem, ItensVenda)
        Dim qtd As Integer = iten.Quantidade
        Dim preco As Double = iten.Produto.ValorVenda
        e.Row.Cells(3).Text = (qtd * preco).ToString("C")

    End Sub
End Class